

from data_define import Record

class FileReader:
    """文件读取器
    """

    def read_data(self) -> list[Record]:
        """读取文件数据，转换成Record记录对象的链表
        """
        pass


class CSV_Reader(FileReader):
    """CSV文件读取器
    """

    path:str = None # 文件路径

    def __init__(self, path:str):
        super().__init__()
        self.path = path

    def read_data(self) -> list[Record]:
        """读取文件数据

        Returns:
            list[Record]: 记录链表
        """
        f = open(self.path, "r", encoding="UTF-8")
        str_list = f.readlines()
        f.close()

        record_list:list[Record] = []
        for line in str_list:
            line = line.strip() # 消除每一行中的'\n'
            data_list = line.split(",")
            record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
            record_list.append(record)        

        return record_list

 
import json

class JsonReader(FileReader):
    """Json文件读取器
    """
    path:str = None # 文件路径

    def __init__(self, path:str):
        super().__init__()
        self.path = path

    def read_data(self) -> list[Record]:
        """读取文件数据

        Returns:
            list[Record]: 记录链表
        """
        f = open(self.path, "r", encoding="UTF-8")
        str_list = f.readlines()
        f.close()

        record_list:list[Record] = []
        for line in str_list:
            data_dict = json.loads(line)
            record = Record(data_dict["date"], data_dict["order_id"], int(data_dict["money"]), data_dict["province"])
            record_list.append(record)

        return record_list

if __name__ == '__main__':
    print("开始测试 file_define.py")
    # csv_1 = CSV_Reader(".\\销售数据\\2011年1月销售数据.csv")
    # record_list_1 = csv_1.read_data()
    # for it_record in record_list_1:
    #     print(it_record.date, it_record.order_id, it_record.money, it_record.province)
    json_1 = JsonReader(".\\销售数据\\2011年2月销售数据JSON.txt")
    json_data = json_1.read_data()
    for it_record in json_data:
        print(it_record)

    